﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
using System.ServiceModel;

namespace NCacheDServer
{
    public class NCacheDService : ServiceBase
    {
        private ServiceHost serviceHost = null;

        public NCacheDService()
        {
            // Name the Windows Service
            ServiceName = "NCacheDServer";
        }

        public static void Main()
        {
#if DEBUG
            using (var svc = new NCacheDService())
            {
                Console.WriteLine("Starting NCacheDService...");
                svc.OnStart(null);
                Console.WriteLine("Started NCacheDService...");
                Console.WriteLine("Press any key to stop the service...");
                Console.ReadKey();
                svc.OnStop();
            }
#else
            ServiceBase.Run(new NCacheService());
#endif
        }

        protected override void OnStart(string[] args)
        {

            if (serviceHost != null)
            {
                serviceHost.Close();
            }

            // Create a ServiceHost for the CalculatorService type and 
            // provide the base address.
            serviceHost = new ServiceHost(typeof(NCacheDServer));

            // Open the ServiceHostBase to create listeners and start 
            // listening for messages.
            serviceHost.Open();

        }
        protected override void OnStop()
        {
            if (serviceHost != null)
            {
                serviceHost.Close();
                serviceHost = null;
            }
        }
    }

}
